package com.szt.ynb.server.router.controller.manage;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.szt.ynb.server.router.annotations.Log;
import com.szt.ynb.server.router.entity.Server;
import com.szt.ynb.server.router.pojo.SessionUser;
import com.szt.ynb.server.router.service.ServerService;
import com.szt.ynb.server.router.vo.base.RepBaseVO;

@Controller
@RequestMapping("manage/server")
public class ManageServerController {
	
	@Autowired
	private ServerService serverService;
	
	/***
	 * 服务器列表
	 * 
	 * @param model
	 * @return
	 */
	@Log(value = "查看服务器列表")
	@GetMapping("list")
	public String listPage(Model model) {
		List<Server> servers = serverService.findAll();
		model.addAttribute("servers", servers);
		return "list";
	}
	
	/***
	 * 强制下线
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("status/forceoffline")
	@ResponseBody
	public RepBaseVO forceOffline(@ModelAttribute SessionUser sessionUser, Long id) {
		RepBaseVO repVO = new RepBaseVO<>();
		Server server = serverService.findById(id);
		if (server != null) {
			server.setStatus(Server.STATUS_FORSE_OFFLINE);
			server.setUpdateDate(new Date());
			serverService.save(server);
		}
		return repVO.setCommonSuccess();
	}
	
	/***
	 * 重置状态
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("status/reset")
	@ResponseBody
	public RepBaseVO resetStatus(@ModelAttribute SessionUser sessionUser, Long id) {
		RepBaseVO repVO = new RepBaseVO<>();
		Server server = serverService.findById(id);
		if (server != null) {
			server.setStatus(Server.STATUS_DEFAULT);
			server.setUpdateDate(new Date());
			serverService.save(server);
		}
		return repVO.setCommonSuccess();
	}
	
}
